package com.zzyl.nursing.service.impl;

import java.time.Duration;
import java.util.List;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.vo.NursingLevelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.service.INursingLevelService;

/**
 * 护理等级Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-07
 */
@Service
@Slf4j
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper,NursingLevel> implements INursingLevelService
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    private static final String NURSING_LEVEL_KEY = "NursingLevel";


    @Override
    public List<NursingLevel> getAll() {
        //Redis使用思路: 先去Redis查询，如果没有，就去数据库查，然后把结果在返回之前写Redis里面去，如果Reids有，就直接返回，不用再查数据库了

        String s = redisTemplate.opsForValue().get(NURSING_LEVEL_KEY);
        if (StringUtils.hasText(s)){
            log.info("缓存中存在,不需要查询数据库");
            return JSONUtil.toList(s,NursingLevel.class);
        }
            log.info("缓存中不存在,需要查询数据库");

        List<NursingLevel> list = super.list(Wrappers.<NursingLevel>lambdaQuery().eq(NursingLevel::getStatus,1));
        redisTemplate.opsForValue().set(NURSING_LEVEL_KEY,JSONUtil.toJsonStr(list), Duration.ofMinutes(30));
        return list;
    }

    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        return nursingLevelMapper.selectNursingLevelById(id);
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelList(NursingLevel nursingLevel)
    {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        nursingLevel.setCreateTime(DateUtils.getNowDate());
        redisTemplate.delete(NURSING_LEVEL_KEY);
        return nursingLevelMapper.insertNursingLevel(nursingLevel);
    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        nursingLevel.setUpdateTime(DateUtils.getNowDate());
        redisTemplate.delete(NURSING_LEVEL_KEY);
        return nursingLevelMapper.updateNursingLevel(nursingLevel);
    }

    /**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids)
    {
        return nursingLevelMapper.deleteNursingLevelByIds(ids);
    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {
        redisTemplate.delete(NURSING_LEVEL_KEY);
        return nursingLevelMapper.deleteNursingLevelById(id);
    }
}
